package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.RewardTask;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * 管理员用悬赏任务管理适配器
 */
public class RewardManagementAdapter extends RecyclerView.Adapter<RewardManagementAdapter.ViewHolder> {

    private List<RewardTask> rewardTasks;
    private Context context;
    private OnRewardManagementListener listener;

    /**
     * 管理操作监听接口
     */
    public interface OnRewardManagementListener {
        /**
         * 查看任务详情
         */
        void onViewDetail(RewardTask task);
        
        /**
         * 编辑任务
         */
        void onEdit(RewardTask task);
        
        /**
         * 删除任务
         */
        void onDelete(RewardTask task);
        
        /**
         * 审核任务
         */
        void onReview(RewardTask task);
    }

    public RewardManagementAdapter(Context context, List<RewardTask> rewardTasks, OnRewardManagementListener listener) {
        this.context = context;
        this.rewardTasks = rewardTasks;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_reward_management, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RewardTask task = rewardTasks.get(position);
        
        // 设置基本信息
        holder.tvTitle.setText(task.getTitle());
        holder.tvReward.setText(String.format(Locale.CHINA, "¥ %.2f", task.getReward()));
        holder.tvDeadline.setText(formatDate(task.getDeadline()));
        
        // 设置状态和操作按钮
        holder.tvStatus.setText(task.getProgressStatusText());
        
        // 根据任务状态设置状态文本颜色和按钮可见性
        switch (task.getProgressStatus()) {
            case RewardTask.STATUS_IN_PROGRESS:
                holder.tvStatus.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                holder.btnReview.setVisibility(View.GONE);
                break;
            case RewardTask.STATUS_UNDER_REVIEW:
                holder.tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_orange_dark));
                holder.btnReview.setVisibility(View.VISIBLE);
                break;
            case RewardTask.STATUS_COMPLETED:
                holder.tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_green_dark));
                holder.btnReview.setVisibility(View.GONE);
                break;
            case RewardTask.STATUS_REJECTED:
                holder.tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
                holder.btnReview.setVisibility(View.GONE);
                break;
        }
        
        // 设置接取情况
        holder.tvTakenCount.setText(String.format(Locale.CHINA, "%d/%d", 
                task.getEarnedPeople(), 
                task.getEarnedPeople() + task.getRemainingOrders()));
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onViewDetail(task);
            }
        });
        
        holder.btnEdit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEdit(task);
            }
        });
        
        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDelete(task);
            }
        });
        
        holder.btnReview.setOnClickListener(v -> {
            if (listener != null) {
                listener.onReview(task);
            }
        });
    }

    @Override
    public int getItemCount() {
        return rewardTasks == null ? 0 : rewardTasks.size();
    }

    /**
     * 更新数据
     */
    public void updateData(List<RewardTask> newTasks) {
        this.rewardTasks = newTasks;
        notifyDataSetChanged();
    }
    
    /**
     * 格式化日期
     */
    private String formatDate(String dateString) {
        if (dateString == null || dateString.isEmpty()) {
            return "未设置";
        }
        
        try {
            if (dateString.contains("T")) {
                SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
                SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                return outputFormat.format(inputFormat.parse(dateString));
            }
            return dateString;
        } catch (Exception e) {
            return dateString;
        }
    }
    
    /**
     * ViewHolder类
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle, tvReward, tvDeadline, tvStatus, tvTakenCount;
        Button btnEdit, btnDelete, btnReview;
        
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_reward_title);
            tvReward = itemView.findViewById(R.id.tv_reward_amount);
            tvDeadline = itemView.findViewById(R.id.tv_reward_deadline);
            tvStatus = itemView.findViewById(R.id.tv_reward_status);
            tvTakenCount = itemView.findViewById(R.id.tv_taken_count);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            btnReview = itemView.findViewById(R.id.btn_review);
        }
    }
} 